package cn.tedu.boot510.controller;

import cn.tedu.boot510.entity.User;
import cn.tedu.boot510.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @Autowired
    UserMapper mapper;
    @RequestMapping("/reg")
    public int reg(@RequestBody User user){
     User u=mapper.select(user.getUsername());
     if(u!=null){
         return 2;
     }
     mapper.insert(user);
     return 1;
    }
    @RequestMapping("/login")
    public int login(@RequestBody User user, HttpSession session){
        User u=mapper.select(user.getUsername());
        if(u!=null){
            if(u.getPassword().equals(user.getPassword())){
                session.setAttribute("user",u);
                return 1;
            }
            return 3;
        }
        return 2;
    }
}
